import { Toast } from "antd-mobile"
import { reqregister } from "../../http/api"

//状态
const initState = {
    //初始化
    user:{
        phone:"",
        nickname:"",
        password:"",
    }
}

//创建types
const types = {
    CHANGE_USER:"REGISTER_CHANGE_USER",
}

//修改user
const reducer =(state=initState,action)=> {
    switch(action.type){
        case types.CHANGE_USER:
            return{
                ...state,
                user:{
                    ...state.user,
                    [action.key]:action.value
                }
            }
        default:
            return state
    }
}

//做修改逻辑
export const actions = {
    //user
    changeUser:(key,value)=>({type:types.CHANGE_USER,key,value}),
    //注册
    reqregister:(push)=>(dispatch,getState)=>{
        let info = getUser(getState())
        reqregister(info).then(res=>{
            if(res.data.code==200){
                Toast.show({
                    content:res.data.msg
                })
                push("/login")
            }
        })
    }
}

//导出user
export const getUser = state=>state.register.user;


//导出reducer
export default reducer